# 安装bs4

import requests
from bs4 import BeautifulSoup
import csv

# 1.获取网页源代码

url = 'https://movie.douban.com/chart'
header = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.114 Safari/537.36 Edg/103.0.1264.62"
}
resp = requests.get(url,headers=header)

# 2.使用bs4进行解析，拿到数据
# 3.把网页源代码交给Beautifulsoup进行解析，生产bs4对象
page = BeautifulSoup(resp.text,'html.parser')

# 4.从bs4对象中查找数据
# find（标签，属性=属性值）
# find_all（标签，属性=属性值）
# title = page.find_all('tr',class_='item')  #注 意： class是关键字，因此要写成 class_
move = page.find_all('tr',attrs={'class':'item'}) # tr: 代表行  td： 代表列
with open('豆瓣电影排行榜bs4.csv', 'w', encoding='utf-8', newline='') as f:
    csvwrite = csv.writer(f)
    for i in move:
        title = i.find('a',attrs={'class':''}).text.strip(' ,\n').replace(' ','').replace('\n','')
        year_actor = i.find('p',attrs={'class':'pl'}).text
        appraise_num = i.find('span',attrs={'class':'pl'}).text
        if i.find('span',attrs={'class':'rating_nums'}) == -1:
            score = '暂无评分'
        else:
            score = i.find('span', attrs={'class': 'rating_nums'}).text
        dic = {'title':title,'year_actor':year_actor,'score':score,'appraise_num':appraise_num}
        csvwrite.writerow(dic.values())
print('over!')
